package websocket

import (
	"fmt"
	"math/rand"
	"peilian-api/app/global/databases/tables"
	"peilian-api/app/global/variable"
)

type CustomClient struct {
	*Client
}

func (c *CustomClient) getTotal() int {
	return c.Bus.Session.Career.QuestionNum
}

func (c *CustomClient) CacheNextQues(client Client) error {
	return nil
}

func (c *CustomClient) NextQuestion(int) []byte {
	c.Bus.CurrentTag += 1
	return c.GetCustomQuestion()
}

func (c *CustomClient) GetCustomQuestion() []byte {
	questions := make([]*tables.Question, 0)
	subQuery := variable.DB.MySQL.Model(&tables.CustomCareer{}).Select("QuestionID").
		Where("career_id=? and tag=?", c.Bus.Session.CareerID, c.Bus.CurrentTag)

	if err := variable.DB.MySQL.Select("ID", "Video", "Text", "PcVideo", "QuestionOrigin").
		Where("id in (?)", subQuery).Find(&questions).Error; err != nil {
		return c.ResClient("error", &VideoInfo{}, err.Error())
	}
	if len(questions) <= 0 {
		return c.ResClient("error", &VideoInfo{}, fmt.Sprintf("题库中没有定义第[%d]道题的题目", c.Bus.CurrentTag))
	}
	randSub := rand.Intn(len(questions))
	q := questions[randSub]
	c.Bus.CurrentQuestion = q
	if err1 := c.CreateRealQues(nil); err1 != nil {
		return err1
	}
	return c.ResClient("ask", &VideoInfo{
		Video:          q.Video,
		PcVideo:        q.PcVideo,
		Text:           q.Text,
		QuestionOrigin: q.QuestionOrigin,
	})
}

func (c *CustomClient) FirstQuesVideo() []byte {
	c.Bus.CurrentTag += 1
	return c.GetCustomQuestion()
}

func (c *CustomClient) getFinishedStatus() int {
	//variable.DB.MySQL.Model(&tables.Session{}).Where("id = ?", c.Bus.Session.ID).Update("url", "自定义岗位没有面试报告")
	return tables.SucStatus
}
